package com.wells.user.controller;

import com.netflix.discovery.converters.Auto;
import com.wells.user.constant.CookieConstant;
import com.wells.user.constant.RedisConstant;
import com.wells.user.domian.UserInfo;
import com.wells.user.enums.ResultEnum;
import com.wells.user.enums.RoleEnum;
import com.wells.user.repository.UserInfoRepository;
import com.wells.user.service.UserService;
import com.wells.user.utils.CookieUtil;
import com.wells.user.utils.JsonUtil;
import com.wells.user.utils.ResultVoUtil;
import com.wells.user.vo.ResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 施永泉
 * @Date: 18/8/11 18:06
 * @Description:
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登录
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/buyer")
    public ResultVo buyer(@RequestParam(name = "openid") String openid,
                          HttpServletResponse response){
        //1. openid和数据库里的数据匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null){
            return ResultVoUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //2. 判断角色
        if (!userInfo.getRole().equals(RoleEnum.BUYER.getCode())){
            return ResultVoUtil.error(ResultEnum.ROLE_ERROR);
        }

        //3. cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID, userInfo.getOpenid(), CookieConstant.EXPIRE);
        return ResultVoUtil.success();
    }

    /**
     * 卖家登录
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/seller")
    public ResultVo seller(@RequestParam(name = "openid") String openid,
                           HttpServletResponse response,
                           HttpServletRequest request){
        //判断是否已登录
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null && StringUtils.isNotBlank(stringRedisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())))){
            return ResultVoUtil.success();
        }

        //1. openid和数据库里的数据匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null){
            return ResultVoUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //2. 判断角色
        if (!userInfo.getRole().equals(RoleEnum.SELLER.getCode())){
            return ResultVoUtil.error(ResultEnum.ROLE_ERROR);
        }

        //3.redis设置key=UUID ,value=xyz
        String token =  UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE, token),
                userInfo.getOpenid(),
                RedisConstant.EXPIRE,
                TimeUnit.SECONDS);

        //4. cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.EXPIRE);
        return ResultVoUtil.success();
    }
}
